//
//  TZStatues.swift
//  WeiboWithSwift
//
//  Created by 涂真 on 15/12/22.
//  Copyright © 2015年 涂真. All rights reserved.
//

import UIKit

class TZStatus: NSObject {
    /// 1.定义模型属性
    
    // MARK: - 属性
    /// 创建时间
    var created_at: String?
    
    /// 微博id
    var id: Int64 = 0
    
    /// 微博正文内容
    var text: String?
    
    /// 来源
    var source: String?
    
    ///微博的配图 [String: AnyObject]
    var pic_urls:[[String: AnyObject]]?
    
    /// 转发数
    var reposts_count: Int = 0
    
    /// 评论数
    var comments_count: Int = 0
    
    /// 表态数(点赞数)
    var attitudes_count: Int = 0
    
    /// 用户模型
    var user: TZUser?
    
    //MARK: - 字典转模型
    ///字典转模型
    init(dict:[String : AnyObject]) {
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    //字典的key在模型中找不到对应的属性,需要实现 forUndefinedKey
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
            if let dic = value as? [String:AnyObject]{
                self.user = TZUser(dict: dic)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
    //实现
    override var description:String{
        let prop = ["created_at", "id", "text", "source", "pic_urls", "reposts_count", "comments_count", "attitudes_count","user"]
        let dic = dictionaryWithValuesForKeys(prop)
        return "\n\t微博模型\(dic)"
    }
    
    //MARK: - 通过TZNetWorkTool获取微博原实际数据
    ///获取模型数据
    class func loadStatuses(finished:(statuses:[TZStatus]?,error:NSError?)->())->(){
        TZNetWorkTool.shardNetWorkTool.loadStatus { (result, error) -> () in
            if result == nil || error != nil{
                //数据获取失败
                finished(statuses: nil, error: error)
                return
            }
            if let arr = result?["statuses"] as? [[String:AnyObject]]{
                var statuses = [TZStatus]()
                for dict in arr{
                    let status = TZStatus(dict: dict)
                    statuses.append(status)
                }
                finished(statuses: statuses, error: nil)
            }else{
                finished(statuses: nil, error: nil)
            }
        }
    }
}
